package com.carl.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.carl.bean.UserInfo;
import com.carl.service.UserService;
import com.carl.sysutil.StringUtil;


@Controller
public class UserAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 826946298855097038L;

	public UserInfo user;
	public boolean success = true;
	public UserService userSer;
	public String outMsg;
	
	public String loginOutAction(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession sysSession = request.getSession();
		
		sysSession.invalidate();
		
		outMsg = "success";
		return SUCCESS;
	}
	
	/**
	 * 用户登录的方法
	 * @return
	 */
	public String userLoginAction(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession sysSession = request.getSession(true);
		
		
		//首先进行证书校验，看证书是否有效
		boolean licValid = userSer.judgeLiceIsValid();
		if(!licValid){
			outMsg = StringUtil.THE_LICENSE_CHECK_ERROR;
			return SUCCESS;
		}
		
		//进行登录用户信息的相关校验
		
		//所有校验都成功之后就将用户的相关信息存入session中
		sysSession.setAttribute("userName", user.getUserName());
		sysSession.setAttribute("role", StringUtil.THE_ROLE_OF_USER_IS_SYS);
		return SUCCESS;
	}
	
	public String saveUserInfoAction(){
		
		int result = userSer.saveUserInfo(user);
		if(result != UserInfo.USER_OPER_SUCCESS){
			outMsg = String.valueOf(result);
		}
		
		return SUCCESS;
	}

	@Resource
	public void setUserSer(UserService userSer) {
		this.userSer = userSer;
	}

	public void setUser(UserInfo userInfo) {
		this.user = userInfo;
	}
	public boolean isSuccess() {
		return success;
	}
	public String getOutMsg() {
		return outMsg;
	}
}
